package com.foreverwin.jzymes.common.util;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.service.ISystemRuleAdviceService;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.framework.util.SpringUtils;
import org.springframework.util.Assert;

public class CarrierUtil {

    private static boolean initialized = false;
    private static int startIndex = 0;
    private static int carrierLength = 0;
    private static ISystemRuleAdviceService systemRuleAdviceService;

    public static void initSystemRule( String site ){
        systemRuleAdviceService = SpringUtils.getBean( ISystemRuleAdviceService.class );
        startIndex = Integer.parseInt(systemRuleAdviceService.findSystemRule( site, Constants.SYSTEM_RULE_CARRIER_START_INDEX, "1" ));
        carrierLength = Integer.parseInt(systemRuleAdviceService.findSystemRule( site, Constants.SYSTEM_RULE_CARRIER_LENGTH, "10" ));

    }

    public static String buildCarrier( String site, String carrier ){
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ) );

        initSystemRule( site );
        Assert.isTrue( carrier.length() == carrierLength, I18nUtil.getI18nText( "common.carrierNotValid", new String[]{ carrier } ) );

        return carrier.substring( startIndex, carrierLength );
    }
}
